
#include "kodes.h"
#include "latin3.h"
#include <boost/assert.hpp>


const wchar_t *ElLatin3::getUnicodeMap()
{
  static const wchar_t UnicodeMap[128] = {  // ISO 8859-3 / Latin 3 / CP1254 Encoding
    0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
    0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F,
    0x00A0, 0x0126, 0x02D8, 0x00A3, 0x00A4, 0x0000, 0x0124, 0x00A7, 0x00A8, 0x0130, 0x015E, 0x011E, 0x0134, 0x00AD, 0x0000, 0x017B,
    0x00B0, 0x0127, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x0125, 0x00B7, 0x00B8, 0x0131, 0x015F, 0x011F, 0x0135, 0x00BD, 0x0000, 0x017C,
    0x00C0, 0x00C1, 0x00C2, 0x0000, 0x00C4, 0x010A, 0x0108, 0x00C7, 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF,
    0x0000, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x0120, 0x00D6, 0x00D7, 0x011C, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x016C, 0x015C, 0x00DF,
    0x00E0, 0x00E1, 0x00E2, 0x0000, 0x00E4, 0x010B, 0x0109, 0x00E7, 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
    0x0000, 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x0121, 0x00F6, 0x00F7, 0x011D, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x016D, 0x015D, 0x02D9 };
  return UnicodeMap;
}


wchar_t ElLatin3::doni()
{
  unsigned char b = doniBajton();
  if (b <= 0x7f)
    return static_cast<wchar_t>(b);

  size_t index = b & 0x7f;
  wchar_t res = getUnicodeMap()[index];
  return res;
}



void AlLatin3::meti(wchar_t c)
{
  if (c <= 0x7f)
  {
    metiBajton(static_cast<unsigned char>(c));
    return;
  }

  if (latin3map.empty())
  {
    for (size_t i=0; i<0x80; i++)
      latin3map[ElLatin3::getUnicodeMap()[i]] = static_cast<unsigned char>(0x80+i);
  }

  auto it = latin3map.find(c);
  if (it != latin3map.end())
  {
    metiBajton(it->second);
    return;
  }

  BOOST_ASSERT(false);
  throw runtime_error("Can't encode char");
}

